抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

这周在正式开发时碰到了一个客户提出来的要求,需要在自定义屏幕的按钮工具栏处实现一个Radio Button,用以显示不同的数据内容。当时想用静态文本然后通过隐藏按钮的方式实现,但是查询和询问一部分同事后发现使用创建按钮时的Dynamic Text的方式更加合适。现将此方式记录如下。其实OOALV也有类似的方式。

SAP GUI Dynamic Text

一、实现方式

1.先创建一个程序、屏幕和GUI Status

1.创建程序屏幕与GUI Status

2.创建动态按钮

在新建的GUI Status中,填写动态按钮的Function Code等信息。然后可以从中打开定义动态按钮样式的帮助文档,其中介绍了用于设置按钮样式的结构体类型:SMP_DYNTXT

  • TEXT:功能或菜单文本。此文本显示在菜单栏、菜单和Possible的功能列表中。只要ICON_ID组件为空,它也会出现在应用程序工具栏中。
  • ICON_ID 和 ICON_TEXT:如果要在按钮上显示图标,则必须填写icon_ID。您可以在icon_text中输入其他图标文本。通过为ICON_ID字段分配一个图标来填充该字段(例如,F-ICON_ID=ICON_CANCEL)。您不能在此处使用ICON_CREATE功能模块。
    如果ICON_ID组件包含一个值,您还必须填写TEXT组件,因为它的内容显示在Possible的函数列表中。
  • QUICKINFO:此文本用作状态行中的快速信息。
2.创建动态按钮的Function Code 3.动态按钮结构体类型

3.在TOP中创建按钮样式结构体

现在TOP中创建全局变量GS_RADBUT_ONE 和GS_RADBUT_TWO两个按钮样式变量。

1
2
3
DATA:
GS_RADBUT_ONE TYPE SMP_DYNTXT,
GS_RADBUT_TWO TYPE SMP_DYNTXT.

然后将其嵌入到参照字段中。因为是定义的全局变量,所以选择Program field选项。

4.绑定样式控制结构体

选择程序字段后会弹框的显示程序中你定义的所有程序字段,在其中选择之前定义好的字段即可,在选择前记得激活程序。

5.样式控制结构体选择界面 15.补充图片 6.绑定样式按钮

剩下的操作基本就和普通的创建按钮流程是一致的。

7.选择响应快捷键

到了下面这一步就算是绑定完成了,当然也可以再通过下面的Change Text Type将其转换为静态按钮。

8.绑定最终确定 9.最终绑定好结果展示

4.设置按钮样式与切换逻辑代码

因为设置按钮的样式是Radio Button的样式,而这些样式又都存储在ICON表中,所以查询样式如下:

10.查询按钮图标选择屏幕 11.Radiobutton Result Screen

5.编写样式和切换的完整代码逻辑

(1)Include TOP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
*----------------------------------------------------------------------*
* Constants
*----------------------------------------------------------------------*
CONSTANTS:
GC_X TYPE C LENGTH 1 VALUE 'X'.

*----------------------------------------------------------------------*
* Variables
*----------------------------------------------------------------------*
DATA:
GV_OKCODE TYPE SY-UCOMM,
GV_SAVEOK TYPE SY-UCOMM,
GV_RAD_ONE TYPE C,
GV_RAD_TWO TYPE C.

*----------------------------------------------------------------------*
* Structure
*----------------------------------------------------------------------*
DATA:
GS_RADBUT_ONE TYPE SMP_DYNTXT,
GS_RADBUT_TWO TYPE SMP_DYNTXT.
(2)Main Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
*&---------------------------------------------------------------------*
*& Report YTEST_PROGRAM_
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YTEST_PROGRAM_ .

*----------------------------------------------------------------------*
* Include
*----------------------------------------------------------------------*
INCLUDE YTEST_PROGRAM_ _TOP.
INCLUDE YTEST_PROGRAM_ _SCR.
INCLUDE YTEST_PROGRAM_ _CLA.
INCLUDE YTEST_PROGRAM_ _PBO.
INCLUDE YTEST_PROGRAM_ _PAI.
INCLUDE YTEST_PROGRAM_ _F01.

*----------------------------------------------------------------------*
* INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.

*----------------------------------------------------------------------*
* END-OF-SELECTION
*----------------------------------------------------------------------*
END-OF-SELECTION.
GV_RAD_ONE = GC_X.
IF SY-BATCH IS INITIAL.
CALL SCREEN 0100.
ENDIF.
(3)Include PBO
1
2
3
4
5
6
7
8
9
10
11
12
13
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
PERFORM SET_RADIOBUTTON_STYLE USING GV_RAD_ONE TEXT-R01
CHANGING GS_RADBUT_ONE.
PERFORM SET_RADIOBUTTON_STYLE USING GV_RAD_TWO TEXT-R02
CHANGING GS_RADBUT_TWO.
SET PF-STATUS 'G0100'.
* SET TITLEBAR 'xxx'.
ENDMODULE.
(4)Include PAI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
PERFORM USER_COMMAND_0100.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module EXIT INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE EXIT INPUT.
PERFORM EXIT.
ENDMODULE.
(5)Include F01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
*&---------------------------------------------------------------------*
*& Form SET_RADIOBUTTON_STYLE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> PV_FLAG
*& --> PV_TEXT
*& <-- PS_RADBUT
*&---------------------------------------------------------------------*
FORM SET_RADIOBUTTON_STYLE USING VALUE(PV_FLAG)
VALUE(PV_TEXT)
CHANGING VALUE(PS_RADBUT) TYPE SMP_DYNTXT.
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_SELECT TYPE ICON-ID VALUE '@TB@',
LC_UNSELECT TYPE ICON-ID VALUE '@SR@'.
*----------------------------Logic-------------------------------------*
PS_RADBUT-ICON_TEXT = PV_TEXT.
PS_RADBUT-QUICKINFO = PV_TEXT.

IF PV_FLAG IS NOT INITIAL.
PS_RADBUT-ICON_ID = LC_SELECT.
ELSE.
PS_RADBUT-ICON_ID = LC_UNSELECT.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form USER_COMMAND_0100
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM USER_COMMAND_0100 .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_RADONE TYPE SY-UCOMM VALUE 'RADONE',
LC_RADTWO TYPE SY-UCOMM VALUE 'RADTWO'.
*----------------------------Logic-------------------------------------*
CLEAR: GV_SAVEOK.
GV_SAVEOK = GV_OKCODE.
CLEAR: GV_OKCODE.

CASE GV_SAVEOK.
WHEN LC_RADONE.
IF GV_RAD_ONE IS INITIAL.
GV_RAD_ONE = GC_X.
CLEAR GV_RAD_TWO.
ENDIF.
WHEN LC_RADTWO.
IF GV_RAD_TWO IS INITIAL.
GV_RAD_TWO = GC_X.
CLEAR GV_RAD_ONE.
ENDIF.
WHEN OTHERS.
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form EXIT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM EXIT .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_BACK TYPE SY-UCOMM VALUE 'BACK',
LC_EXIT TYPE SY-UCOMM VALUE 'EXIT',
LC_CANC TYPE SY-UCOMM VALUE 'CANC'.
*----------------------------Logic-------------------------------------*
CLEAR: GV_SAVEOK.
GV_SAVEOK = GV_OKCODE.
CLEAR: GV_OKCODE.

CASE GV_SAVEOK.
WHEN LC_BACK OR LC_EXIT OR LC_CANC.
LEAVE TO SCREEN 0.
WHEN OTHERS.
ENDCASE.
ENDFORM.

6.展示效果

(1)初始化页面
12.程序结果画面初始
(2)点击Radio Button One
12.程序结果画面初始
(3)点击Radio Button Two
13.点击按钮结果展示

二、OOALV的动态按钮实现方式

此处提供一个思路,在定义OOALV的Toolbar事件时,通过类似于上面的方式在Toolbar里面修改按钮样式即可实现。

1.实现部分参考代码

(1)Include TOP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
*----------------------------------------------------------------------*
* Constants
*----------------------------------------------------------------------*
CONSTANTS:
GC_X TYPE C LENGTH 1 VALUE 'X',
GC_S TYPE C LENGTH 1 VALUE 'S',
GC_E TYPE C LENGTH 1 VALUE 'E',
GC_NULL TYPE C LENGTH 1 VALUE ''.

*----------------------------------------------------------------------*
* Variables
*----------------------------------------------------------------------*
DATA:
GV_MESSAGE TYPE C,
GV_SAVEOK TYPE SY-UCOMM,
OK_CODE TYPE SY-UCOMM,
GV_RAD_ONE TYPE C,
GV_RAD_TWO TYPE C.

*----------------------------------------------------------------------*
* Structure
*----------------------------------------------------------------------*
DATA:
BEGIN OF GS_TABLE,
FIELD,
END OF GS_TABLE.

*----------------------------------------------------------------------*
* Internal Table
*----------------------------------------------------------------------*
DATA:
GT_TABLE LIKE TABLE OF GS_TABLE,
GT_FIELDCATALOG TYPE LVC_T_FCAT.

*----------------------------------------------------------------------*
* Object
*----------------------------------------------------------------------*
CLASS LCL_EVENTS DEFINITION DEFERRED.
DATA:
GO_EVENTS TYPE REF TO LCL_EVENTS,
GO_ALV TYPE REF TO CL_GUI_ALV_GRID,
GO_CONTAINER TYPE REF TO CL_GUI_DOCKING_CONTAINER.
(2)Include CLA
1
2
3
4
5
6
7
8
9
10
11
12
CLASS LCL_EVENTS DEFINITION.
PUBLIC SECTION.
METHODS:
HANDLE_TOOLBAER FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID
IMPORTING E_OBJECT E_INTERACTIVE.
ENDCLASS.

CLASS LCL_EVENTS IMPLEMENTATION.
METHOD HANDLE_TOOLBAER.
PERFORM HANDLE_TOOLBAER CHANGING E_OBJECT E_INTERACTIVE..
ENDMETHOD.
ENDCLASS.
(3)Form HANDLE_TOOLBAER
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
*&---------------------------------------------------------------------*
*& Form HANDLE_TOOLBAER
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& <-- E_OBJECT
*& <-- E_INTERACTIVE
*&---------------------------------------------------------------------*
FORM HANDLE_TOOLBAER CHANGING
VALUE(PO_OBJECT) TYPE REF TO CL_ALV_EVENT_TOOLBAR_SET
VALUE(PV_INTERACTIVE).
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_RADONE TYPE SY-UCOMM VALUE 'RADONE',
LC_RADTWO TYPE SY-UCOMM VALUE 'RADTWO'.
DATA:
LS_TOOLBAER TYPE STB_BUTTON,
LV_ICON TYPE ICON-NAME.
*----------------------------Logic-------------------------------------*
"Set the vertical line that distinguishes the button area
LS_TOOLBAER-BUTN_TYPE = 3.
APPEND LS_TOOLBAER TO PO_OBJECT->MT_TOOLBAR.

"Radio Button One
IF GV_RAD_ONE IS NOT INITIAL.
PERFORM SET_TOOLBAER USING LC_RADONE ICON_WD_RADIO_BUTTON
TEXT-R01 TEXT-R01
CHANGING PO_OBJECT.
ELSE.
PERFORM SET_TOOLBAER USING LC_RADONE ICON_WD_RADIO_BUTTON_EMPTY
TEXT-R01 TEXT-R01
CHANGING PO_OBJECT.
ENDIF.

"Radio Button Two
IF GV_RAD_TWO IS NOT INITIAL.
PERFORM SET_TOOLBAER USING LC_RADONE ICON_WD_RADIO_BUTTON
TEXT-R02 TEXT-R02
CHANGING PO_OBJECT.
ELSE.
PERFORM SET_TOOLBAER USING LC_RADONE ICON_WD_RADIO_BUTTON_EMPTY
TEXT-R02 TEXT-R02
CHANGING PO_OBJECT.
ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_TOOLBAER
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> PV_FUNC
*& --> PV_ICON
*& --> PV_QUINFO
*& --> PV_TEXT
*& <-- PO_OBJECT
*&---------------------------------------------------------------------*
FORM SET_TOOLBAER USING VALUE(PV_FUNC)
VALUE(PV_ICON)
VALUE(PV_QUINFO)
VALUE(PV_TEXT)
CHANGING
VALUE(PO_OBJECT) TYPE REF TO CL_ALV_EVENT_TOOLBAR_SET.
*--------------------------Variables-----------------------------------*
DATA:
LS_TOOLBAER TYPE STB_BUTTON.
*----------------------------Logic-------------------------------------*
CLEAR: LS_TOOLBAER.
LS_TOOLBAER-FUNCTION = PV_FUNC.
LS_TOOLBAER-ICON = PV_ICON.
LS_TOOLBAER-TEXT = PV_TEXT.
LS_TOOLBAER-QUICKINFO = PV_QUINFO.
APPEND LS_TOOLBAER TO PO_OBJECT->MT_TOOLBAR.
ENDFORM.

响应切换的代码和上面的PAI逻辑一致,但是是写在OOALV的User Command事件中的。

2.结果截图

14.OOALV实现结果画面

三、参考资料

DYNAMIC ICON/TEXT FOR PF STATUS BUTTON

评论